package excel

import (
	"strconv"

	"github.com/xuri/excelize/v2"
)

// 测试导出
func (export *DocumentExport) Test01() {

	// 性别Sheet
	dict_sheet := "dict_gender"
	dict_gender := []any{"男", "女", "多", "少", "玲"}

	export.file.NewSheet(dict_sheet)
	for index, v := range dict_gender {
		cellName, _ := excelize.CoordinatesToCellName(1, index+1)
		export.file.SetCellValue(dict_sheet, cellName, v)
	}

	// 数据Sheet
	data_sheet := "Sheet1"
	sw, _ := export.file.NewStreamWriter(data_sheet)

	// 数据表头(A1位置)
	headers := []any{"ID", "名称", "性别", "职位", "等级"}
	cellName, e := excelize.CoordinatesToCellName(1, 1)
	if e == nil {
		sw.SetRow(cellName, headers)
	}

	// 添加下拉数据验证
	dv := excelize.NewDataValidation(true)
	// 设置应用验证的单元格范围
	dv.SetSqref("C1:C1048576")
	// 设置下拉列表的选项的范围(ROW)
	dv.SetSqrefDropList(dict_sheet + "!$A$1:$A" + strconv.Itoa(len(dict_gender)))
	// 将数据验证规则应用到指定的工作表
	export.file.AddDataValidation(data_sheet, dv)

	// 写入数据(A2开始)
	for row := 1; row <= 20; row++ {
		v := []any{9528 + row, "比目", "男", "司机", "3"}

		// 处理字典转换????
		// 比如gender=1 转换为`男`
		// 比如gender=2 转换为`女`

		cellName, e := excelize.CoordinatesToCellName(1, row+1)
		if e == nil {
			sw.SetRow(cellName, v)
		}
	}

	sw.Flush()
}
